package com.handwer.tools.stopuseyourphone;

import android.app.NotificationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddActivity extends AppCompatActivity implements View.OnClickListener {

    Button Start;
    Button Stop;
    EditText Use;
    EditText Relax;
    TextView Progress;
    MyCountDownTimer mc1;
    MyCountDownTimer2 mc2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Start = (Button) findViewById(R.id.button4);
        Progress = (TextView) findViewById(R.id.textView5);
        Use = (EditText) findViewById(R.id.editText);
        Relax = (EditText) findViewById(R.id.editText2);
        Stop = (Button) findViewById(R.id.button2);
        Start.setOnClickListener(this);
        Stop.setOnClickListener(this);
        if (Use.getText().toString() == ""){
            Use.setText("30");
        }
        if (Relax.getText().toString() == ""){
            Relax.setText("10");
        }
        mc1 = new MyCountDownTimer(1800000, 1);
        mc2 = new MyCountDownTimer2(600000,1);

    }

    @Override
    public void onClick(View v) {
        if (v == Start){
            StartTimer1();
        }else if (v == Stop){
            StopTimer1();
            StopTimer2();
        }
    }
     public class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture    表示以毫秒为单位 倒计时的总数
         *                          <p>
         *                          例如 millisInFuture=1000 表示1秒
         * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法
         *                          <p>
         *                          例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

         @Override
         public void onTick(long millisUntilFinished) {
             Log.i("onTick",millisUntilFinished+"");
             Progress.setText(millisUntilFinished + getString(R.string.seconds));
         }

         @Override
         public void onFinish() {
            Progress.setText(R.string.thirty_minutes);
             StartTimer2();
         }
     }
    public class MyCountDownTimer2 extends CountDownTimer {
        /**
         * @param millisInFuture    表示以毫秒为单位 倒计时的总数
         *                          <p>
         *                          例如 millisInFuture=1000 表示1秒
         * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法
         *                          <p>
         *                          例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         */
        public MyCountDownTimer2(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            Log.i("onTick",millisUntilFinished+"");
            Progress.setText(millisUntilFinished + "");
        }

        @Override
        public void onFinish() {
            Progress.setText(R.string.ten_minutes);
            StartTimer1();
        }
    }
    public void StartTimer1(){
        mc1.start();
    }
    public void StopTimer1(){
        mc1.cancel();
    }
    public void StartTimer2(){
        mc2.start();
    }
    public void StopTimer2(){
        mc2.cancel();
    }
}
